// pages/search/search.js
const host=getApp().host;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    admin:{},
    role:{},
    functionList:[],
    noticeBG:'', // 背景
    notices:[], // 公告通知
    worker:false, // 是否为工作人员
  },


  // 格式化日期
  formatDate(row) {
    let date = new Date(row.date);
    let month =
          date.getMonth() + 1 < 10
                ? "0" + (date.getMonth() + 1)
                : date.getMonth() + 1;
    let day =
          date.getDate() < 10
                ? "0" + date.getDate()
                : date.getDate();
    return (
          date.getFullYear() +
          "-" +
          month +
          "-" +
          day 
    );
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let token=wx.getStorageSync('token')||null;
    if(token==null){
      wx.showModal({
        title: '提示',
        content: '请先登录',
        showCancel:false,
        success:res=> {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login'
            })
          }
        }
      })
    }else{
      this.init()
    }
  },

  /**
   * 初始化功能列表
   */
  initFunction(){
    let list=[
      {
        id:'epidemic',
        url:'/pages/excused/excused',
        icon:"icon-qingjiatongji-01",
        iconStyle:'epidemic-icon',
        title:'学生请假',
        subtitle:'Student Asks For Leave',
        date:''
      },
      {
        id:'leave',
        url:'/pages/creative/creative',
        icon:"icon-huodong",
        iconStyle:'epidemic-icon',
        title:'创新创业',
        subtitle:'Entrepreneurial Innovation',
        date:''
      },
      {
        id:'contest',
        url:'/pages/contest/contest',
        icon:"icon-huodongxiangqu",
        iconStyle:'epidemic-icon',
        title:'德育活动',
        subtitle:'Moral Education Activities',
        date:''
      },
    ];

    // 确认登录账户是否为工作人员
    if(this.data.worker){
      list.push(
        {
          id:'verify',
          url:'/pages/verify/verify',
          icon:"icon-saomiao",
          iconStyle:'epidemic-icon',
          title:'工作端',
          subtitle:'Work Side',
          date:''
        },
        {
          id:'dorm',
          url:'/pages/checkDorm/checkDorm',
          icon:"icon-sushe",
          iconStyle:'epidemic-icon',
          title:'学生查宿',
          subtitle:'Students Check Dorm',
          date:''
        },
      )
    }

    this.setData({
      functionList:list
    },()=>{
      wx.hideLoading();
      this.initMoralClassify()
    })
  },

  /**初始化班级德育活动申请 */
  initMoralClassify(){
    wx.request({
      url: host+'client/moralLinkman?user='+wx.getStorageSync('admin').loginName,
      success:res=>{
        let info=res.data.data.info;
        if(info){
          let list=this.data.functionList;
          list.push(
            {
              id:'dorm',
              url:'/pages/classActivities/classActivities',
              icon:"icon-banjihuodong",
              iconStyle:'epidemic-icon',
              title:'班级活动',
              subtitle:'Class Activities',
              date:''
            }
          );
          this.setData({
            functionList:list
          })
        }
      }
    })
  },

  /**初始化首页信息 */
  init(){
    wx.showLoading({
      title: '加载中',
    })
    // 初始化基础信息
    this.setData({
      admin:wx.getStorageSync('admin'),
      role:wx.getStorageSync('role')
    },()=>{
      this.initNotice();
      this.initWorler();
    })
  },

  /**初始化工作人员状态 */
  initWorler(){
    wx.request({
      url: host+'/client/worker/status?loginName='+this.data.admin.loginName,
      success:res=>{
        this.setData({
          worker:res.data.data.status
        },()=>{
          this.initFunction();
        })
      }
    })
  },

  /**初始化公告 */
  initNotice(){
    wx.request({
      url: host+'admin/notice/top',
      method:'GET',
      timeout:10000,
      success:res=>{
        res.data.data.list.forEach(item=>{
          item.date=this.formatTime(item.date);
        })
        this.setData({
          notices:res.data.data.list
        })
      },
      fail:()=>{
        wx.showToast({
          title: '服务器繁忙',
          icon:'error'
        })
      }
    })
  },

  /**格式化时间 */
  formatTime(e){
    var time =  new Date(parseInt(e));
      var y = time.getFullYear();
      var m = time.getMonth()+1;
      var d = time.getDate();
      let t= y+'年'+m+'月'+d+'日';
    return t;
  },

  onLoad(options){
    this.initBg();
  },

  /**初始化背景 */
  initBg(){
        // 请求公告背景图片
        wx.request({
          url:'https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN',
          success: res=>{
              this.setData({
                noticeBG:'https://cn.bing.com'+res.data.images[0].url
              })
          }
        })
  }
})